﻿
// Addition1Dlg.cpp: 实现文件
//

#include "stdafx.h"
#include "Addition1.h"
#include "Addition1Dlg.h"
#include "afxdialogex.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include<string>
#include "resource.h" 
#include "atlconv.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using namespace cv;
using namespace std;

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void On32771();
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
	ON_COMMAND(ID_32771, &CAboutDlg::On32771)
END_MESSAGE_MAP()


// CAddition1Dlg 对话框



CAddition1Dlg::CAddition1Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_ADDITION1_DIALOG, pParent)
	, beijiashu(_T(""))
	, jiashu(_T(""))
	, he(_T(""))

{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAddition1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//  DDX_Text(pDX, IDC_EDIT1,beijiashu);
	//  DDX_Text(pDX, IDC_EDIT2, jiashu);
	//  DDX_Text(pDX, IDC_EDIT3, he);
	DDX_Text(pDX, IDC_EDIT1, beijiashu);
	DDX_Text(pDX, IDC_EDIT2, jiashu);
	DDX_Text(pDX, IDC_EDIT3, he);
	DDX_Control(pDX, IDC_TAB1, m_TabWin);
	//  DDX_Control(pDX, IDC_SLIDER1, m_slider);
	//  DDX_Control(pDX, IDC_EDIT5, m_edit);
	//  DDX_Control(pDX, IDC_SLIDER1, m_slider);
	//  DDX_Text(pDX, IDC_EDIT5, m_edit);
}

BEGIN_MESSAGE_MAP(CAddition1Dlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CAddition1Dlg::OnBnClickedButton1)
	ON_WM_TIMER()
	ON_COMMAND(ID_32771, &CAddition1Dlg::On333333)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &CAddition1Dlg::OnTcnSelchangeTab1)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_JINDUTIAO, &CAddition1Dlg::OnBnClickedJindutiao)
	ON_BN_CLICKED(IDC_SHEZHIZITI, &CAddition1Dlg::OnBnClickedShezhiziti)
	ON_BN_CLICKED(IDC_HANGJIANJU, &CAddition1Dlg::OnBnClickedHangjianju)
	ON_BN_CLICKED(IDC_TONGJI, &CAddition1Dlg::OnBnClickedTongji)
END_MESSAGE_MAP()


// CAddition1Dlg 消息处理程序

BOOL CAddition1Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	static UINT indicators[] = {
	IDS_STRING102,IDS_STRING103,IDS_STRING104
	};
	m_Statusbar.Create(this);                  //创造状态栏
	m_Statusbar.SetIndicators(indicators, 3);   //设置状态栏项目栏数
	CRect rect;
	                          	RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, IDS_STRING104);//指定状态栏在显示窗口位置
	SetTimer(2, 1000, NULL);       //一个1秒触发一次的定时器


	m_Menu.LoadMenuW(IDR_MENU1);//菜单栏ID
	SetMenu(&m_Menu);

	//为Tab Control增加两个页面  
	m_TabWin.InsertItem(0, _T("测试1"));
	m_TabWin.InsertItem(1, _T("测试2"));
	//创建两个对话框  
	m_FirstTab.Create(IDD_DIALOG1, &m_TabWin);
	m_SecondTab.Create(IDD_DIALOG2, &m_TabWin);
	//设定在Tab内显示的范围  
	CRect tabRect;
	m_TabWin.GetClientRect(tabRect);
	tabRect.left += 1;
	tabRect.right -= 1;
	tabRect.top += 22;
	tabRect.bottom -= 1;
	m_FirstTab.MoveWindow(&tabRect);
	m_SecondTab.MoveWindow(&tabRect);
	//显示初始页面  
	m_FirstTab.ShowWindow(SW_SHOW);
	m_SecondTab.ShowWindow(SW_HIDE);


	CSpinButtonCtrl *pSpin;
	pSpin = (CSpinButtonCtrl*)GetDlgItem(IDC_SPIN1);//设置编辑控件为Spin控件的伙伴窗口
	pSpin->SetRange(0, 15);//设置数据范围，上下限
	pSpin->SetPos(3);//设置默认初始值为3，即当前位置
	pSpin->SetBase(10);//10进制 ，设置数值进制数


	namedWindow("view", WINDOW_AUTOSIZE);
	HWND hWnd = (HWND)cvGetWindowHandle("view");
	HWND hParent = ::GetParent(hWnd);
	::SetParent(hWnd, GetDlgItem(IDC_PIC)->m_hWnd);
	::ShowWindow(hParent, SW_HIDE);


	m_Mytip.Create(this);
	m_Mytip.AddTool(GetDlgItem(IDC_EDIT1), _T("请输入被加数")); //IDC_BUTTON为你要添加提示信息的按钮的ID根据ID可以设置多个提示。
	m_Mytip.AddTool(GetDlgItem(IDC_EDIT2), _T("请输入加数"));
	m_Mytip.AddTool(GetDlgItem(IDC_EDIT3), _T("两数之和为"));
	m_Mytip.SetDelayTime(200); //设置延迟
	m_Mytip.SetTipTextColor(RGB(0, 0, 255)); //设置提示文本的颜色
	m_Mytip.SetTipBkColor(RGB(255, 255, 255)); //设置提示框的背景颜色
	m_Mytip.Activate(TRUE); //设置是否启用


	m_pRichEdit = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT21);
	CHARFORMAT cf;                                          //定义字体格式结构体变量
	cf.dwEffects = 0;                                                //字体的效果
	cf.crTextColor = RGB(255, 0, 0);                        //字体的颜色
	cf.yHeight = 500;                                               //字体的大小（并非我们常见的字号概念）
	m_pRichEdit->SetDefaultCharFormat(cf);          //设置控件默认的字体格式



	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CAddition1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CAddition1Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CAddition1Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CAddition1Dlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str1, str2, str3;
	double m, n, p;
	GetDlgItemText(IDC_EDIT1, str1);
	GetDlgItemText(IDC_EDIT2, str2);
	m = _ttof(str1);
	n = _ttof(str2);

	p = m + n;
	str3.Format(_T("1f"), p);
	SetDlgItemText(IDC_EDIT3, str3);


}


void CAddition1Dlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	switch (nIDEvent)
	{


	case 2:   //定时器2位为状态栏时间信息
	{
		CTime t1;
		t1 = CTime::GetCurrentTime();//获取当前系统时间
		m_Statusbar.SetPaneText(2, t1.Format("%Y-%m-%d  %H:%M:%S")); //状态栏显示时间   
		break;
	}
	}

	CDialogEx::OnTimer(nIDEvent);
}


void CAboutDlg::On32771()
{

}

void CAddition1Dlg::On333333()
{
	// TODO: 在此添加命令处理程序代码
	CString picPath;
	//string picpath;
	// TODO: 在此添加控件通知处理程序代码
	  //定义图片路径变量  
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
		OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT, NULL, this);  //选择文件对话框  
	if (dlg.DoModal() == IDOK)
	{
		picPath = dlg.GetPathName();  //获取图片路径  

	}

	USES_CONVERSION;
	string picpath(W2A(picPath));

	Mat pic = imread(picpath);
	CRect rect;
	GetDlgItem(IDC_PIC)->GetClientRect(&rect);
	Rect dst(rect.left, rect.top, rect.right, rect.bottom);
	Mat pic1;
	resize(pic, pic1, Size(rect.Width(), rect.Height()));
	imshow("view", pic1);
	waitKey(0);

}


void CAddition1Dlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	switch (m_TabWin.GetCurSel())
	{
	case 0:
		m_FirstTab.ShowWindow(SW_SHOW);
		m_SecondTab.ShowWindow(SW_HIDE);
		break;
	case 1:
		m_FirstTab.ShowWindow(SW_HIDE);
		m_SecondTab.ShowWindow(SW_SHOW);
		break;
	default:break;
	}
}


BOOL CAddition1Dlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_MOUSEMOVE)
		m_Mytip.RelayEvent(pMsg);
	return CDialogEx::PreTranslateMessage(pMsg);
}



void CAddition1Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CString str3;
	str3.Format(_T("%d"), nPos);
	SetDlgItemText(IDC_EDIT5,str3);

}


void CAddition1Dlg::OnBnClickedJindutiao()
{
	// TODO: 在此添加控件通知处理程序代码
	CProgressCtrl myProCtrl;
	CRect rect, proRect;
	GetClientRect(&rect);
	proRect.left = rect.left + rect.Width() / 2 - 100;
	proRect.top = rect.top + rect.Height() / 2 - 20;
	proRect.right = rect.right - rect.Width() / 2 + 100;
	proRect.bottom = rect.bottom - rect.Height() / 2 + 20;
	//WS_CHILD|WS_VISIBLE|PBS_SMOOTHREVERSE 
	myProCtrl.Create(WS_VISIBLE, proRect, this, 99); //创建位置、大小
	myProCtrl.SetRange(0, 100);//创建一个0-100的进度条
	int i;
	for (i = 0; i <= 100; i++)
	{
		myProCtrl.OffsetPos(1);
		CString str;
		str.Format(_T("%d%%"), i); //百分比
		(GetDlgItem(IDC_JINDUTIAO))->SetWindowText(str);
		Sleep(100);
	}
}



void CAddition1Dlg::OnBnClickedShezhiziti()
{
	// TODO: 在此添加控件通知处理程序代码
	CHARFORMAT cf;
	m_pRichEdit->GetSelectionCharFormat(cf);
	cf.dwMask |= CFM_BOLD;
	cf.dwEffects |= CFE_BOLD;//设置粗体，取消用cf.dwEffects&=~CFE_BOLD;
	cf.dwMask |= CFM_ITALIC;
	cf.dwEffects |= CFE_ITALIC;//设置斜体，取消用cf.dwEffects&=~CFE_ITALIC;
	cf.dwMask |= CFM_UNDERLINE;
	cf.dwEffects |= CFE_UNDERLINE;
	cf.dwMask |= CFM_COLOR;
	cf.crTextColor = RGB(0, 0, 255);//设置颜色
	cf.dwMask |= CFM_SIZE;
	cf.yHeight = 200;//设置高度
	cf.dwMask |= CFM_FACE;
	wcscpy_s(cf.szFaceName, _T("楷书"));//设置字体
	m_pRichEdit->SetSelectionCharFormat(cf);
}


void CAddition1Dlg::OnBnClickedHangjianju()
{
	// TODO: 在此添加控件通知处理程序代码
	PARAFORMAT2   pf;
	memset(&pf, 0, sizeof(pf));
	pf.cbSize = sizeof(PARAFORMAT2);
	pf.dwMask = PFM_LINESPACING | PFM_SPACEAFTER;
	pf.dyLineSpacing = 800;
	pf.bLineSpacingRule = 1;
	m_pRichEdit->SetParaFormat(pf); //控件变量  

}


void CAddition1Dlg::OnBnClickedTongji()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strInput; //MFC的字符串格式
	GetDlgItemText(IDC_RICHEDIT21, strInput);//获得指定控件的句柄，且保存据
	int nLen = strInput.GetLength();        //获得字符串长度
	CString strOutput;
	strOutput.Format(_T("字符串长度：%d,字符个数：%d"), nLen, nLen);//设置式
	SetDlgItemText(IDC_OUTPUT, strOutput); //设置静态文本框输出



}
